package ControlLayer;
import java.util.ArrayList;
import ModelLayer.*;
import java.util.Iterator;
import java.util.*;


/**
 * Controller class for the DVD collection
 * 
 * @author (kbh) 
 * @version (2005.03.31)
 * @revised version English (Ab)
 * (FEN): 2006.03.21: Some changes to the architecture in order to assure layering
 *                    and loose coupling
 */
public class DVDCtr
{
    private CollectionOfDVD collection;
    
    /* Constructor for class DVDCtr */
    public DVDCtr()
    {
        collection = new CollectionOfDVD(); 
    }
    
    /* Method to search the container for a DVD and print information */
    public void findDVD(long id)
    {
        boolean find = true;
        for(DVD dvd : collection.dvds) {
           if(dvd.getId() == id) {           
                System.out.println("NO : " + dvd.getId());
                System.out.println("Title : " + dvd.getTitle());
                System.out.println("Artist : " + dvd.getArtist());
                System.out.println("Publication date : " + dvd.getDate());
           }
        
           else {
                if(find) {
                    System.out.println("No match");
                }
           }     
        }
    }
    
    /* Method to create a DVD and stires ut in the container */
    public void createDVD(long id, String title, String artist, String publicationDate)
    {    
        DVD disk = new DVD(id, title, artist, publicationDate);
            if(collection.dvds.contains(disk)) {
                System.out.println("DVD is already in the collection");
            }
            else {
                collection.dvds.add(disk);
            }
    }
    
    /* Method to delete a DVD from the container */
    public void deleteDVD(long id)
    {
        Iterator<DVD> it = collection.dvds.iterator();
            while(it.hasNext()) {
                DVD d = it.next();
                if(d.getId() == id) {
                    it.remove();
                }
                else {
                    System.out.println("ID invalid");
                }
            }
        //search for the dvd and delete the object from the container
    }
    
    /* Method to create a copy of a specific DVD */
    public void createCopy(long id, int SerialNumber, int PurchaseDate, int PurchasePrice) 
    {
        for(DVD dvd : collection.dvds) {
            if(dvd.getId() == id) {
                Copy disks = new Copy(id, SerialNumber, PurchaseDate, PurchasePrice);
                dvd.getCopies().add(disks);              
            }
        }
    }
     
    /* Print information of all DVDs */
    public void listAllDvds()
    {
          for(DVD dvd : collection.dvds) {
              dvd.printInfo();
            }
    }
    
    /* Method to print copie of a specific DVD */
    public void listCopies(long id)
    {
        Scanner keyboard = new Scanner(System.in); 
        for(DVD dvd : collection.dvds) {
            if(dvd.getId() == id) {
                for(Copy disk : dvd.getCopies()) {
                    disk.printInfo();
                    keyboard.nextLine();
                }
            }
        }
     
    } 
    
    /* To get the dvd ID and the copy ID */
    public Copy getCopy(long dvdId, long copyId)
    {
        return getDVD(dvdId).getCopy(copyId);
    }
    
    /* To see if a this dvd ID is presant  */
    public DVD getDVD(long dvdId)
    {
        for(DVD dvd : collection.dvds)
        {
            if(dvd.getId() == dvdId)
            {
                return dvd;
            }
        }
        return null;
    }
            
}
